﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using SouthernLand.Common.DAL.Photos;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Common.BOL.Photo
{
    public class File
    {
        #region "======Properties====="

        public static int PageIndex; // luu hinh hien tai

        public int RecordID { get; set; }

        public int FileID { get; set; }

        public int CateID { get; set; }

        public string FileName { get; set; }

        public int Width { get; set; }

        public int Height { get; set; }

        public int FileSize { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public int TotalViews { get; set; }

        public DateTime PostDate { get; set; }

        public string Username { get; set; }

        #endregion

        #region "==== Method ===="

        // dem so file theo category
        public static int GetFileCountByCateID(int CateID)
        {
            var db = new FileDAL();
            return db.GetFileCountByCateID(CateID);
        }

        // lay duong dan cua file
        public static string GetPathByFileID(int FileID, PhotoSize Size)
        {
            var db = new FileDAL();
            string path = HttpContext.Current.Server.MapPath("~/Gallery/");
            path = path + db.GetAppKeyByFileID(FileID);

            File obj = db.GetFileByFileID(FileID);

            if (obj.FileName != null && obj.FileName != "")
            {
                if (Size == PhotoSize.Small)
                    path = path + "/Small/" + obj.FileName;
                else
                    path = path + "/Large/" + obj.FileName;
            }
            else
                path = "";

            return path;
        }

        public static List<File> GetFileSlideShowByCateID(int CateID)
        {
            var db = new FileDAL();
            return db.GetFileSlideShowByCateID(CateID);
        }

        // lay file theo Category, co phan trang
        public static List<File> GetFileByCateID_Paging(int CateID, int PageIndex, int PageSize)
        {
            var db = new FileDAL();
            return db.GetFileByCateID_Paging(CateID, PageIndex, PageSize);
        }

        // lay file theo Category
        public static List<File> GetFileByCateID(int CateID)
        {
            var db = new FileDAL();
            return db.GetFileByCateID(CateID);
        }

        // lay file theo 1 CateID ngau nhien 
        public static List<File> GetFileRandomCateID()
        {
            var db = new FileDAL();
            // chi lay cac CateID thuoc loai Album Public
            return db.GetFileByCateID(db.GetRandomCateIDByParentCateID((int) EnumCategory.AlbumsPublic));
        }

        public static File GetFileByFileID(int FileID)
        {
            var db = new FileDAL();
            return db.GetFileByFileID(FileID);
        }

        public static string GetDefaultPhoto(PhotoSize size)
        {
            string path = HttpContext.Current.Server.MapPath("~/Images/");
            switch (size)
            {
                case PhotoSize.Small:
                    path += "placeholder-100.jpg";
                    break;
                case PhotoSize.Medium:
                    path += "placeholder-200.jpg";
                    break;
                case PhotoSize.Large:
                    path += "placeholder-600.jpg";
                    break;
                default:
                    path += "placeholder-600.jpg";
                    break;
            }
            return path;
            //return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        }

        public static ICollection ListUploadDirectory()
        {
            var d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Upload"));
            return d.GetFileSystemInfos("*.jpg");
        }

        public static int Insert(File obj)
        {
            var db = new FileDAL();
            return db.Insert(obj);
        }

        public static int Delete(int FileID)
        {
            try
            {
                File obj = GetFileByFileID(FileID);
                FileProcess.DeleteFile(Globals.PhysicalApplicationPath() + "/UserUpload/Gallery/" + obj.FileName.Trim());

                var db = new FileDAL();
                return db.Delete(FileID);
            }
            catch
            {
                return -1;
            }
        }

        public int Update(int FileID, string Title)
        {
            var db = new FileDAL();
            return db.Update(FileID, Title);
        }

        #endregion
    }
}